package event;

import hibernate.HibernateInitializer;
import hibernate.clientAccount.SingleClient;
import hibernate.hibernateInterface.ClientSystemHibernateInterface;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.api.*;

import utilities.utilities_components.PlainList;
import utilities.utilities_components.SumUp;

public class CSListener implements EventListener{
		
	public CSListener(){}
		
	public void onEvent( Event event ){
		((PlainList)event.getPage().getAttribute("list")).update(
																 getData(
																		 event.getPage().getAttribute("name").toString(),
																		 event.getPage().getAttribute("ageCheck").equals(true),
																		 ((Integer)event.getPage().getAttribute("low_age")).intValue(),
																		 ((Integer)event.getPage().getAttribute("high_age")).intValue(),
																		 (Date)event.getPage().getAttribute("timeFrom"),
																		 (Date)event.getPage().getAttribute("timeTo"),
																		 ((Integer)event.getPage().getAttribute("sex")).intValue(),
																		 (Comboitem)event.getPage().getAttribute("res"),
																		 (Comboitem)event.getPage().getAttribute("roomRes")
																		 )
																);
	}
	
	private List getData(String name,boolean ageChecked,Integer low_age,Integer high_age,
						 Date timeFrom,Date timeTo,int sex,Comboitem searchRes,Comboitem roomRes)
	{
		LinkedList ret = new LinkedList();
		LinkedList resultList = new LinkedList();
		Session session = HibernateInitializer.getSession();
		if (searchRes != null && (searchRes.getContent().equals("房号") && roomRes != null))
		{
			SingleClient sc ;
			try {
				 sc = ClientSystemHibernateInterface.getSingleClientsByRoomNumber(roomRes.getContent(), session);
			}catch(Exception e){return null;}
			if (!sc.getIsCurrentInHotel())
				return null;
			else
			{
				LinkedList col = new LinkedList();
				col.add(sc.getPersonalInfo().getFirstName()+" "+sc.getPersonalInfo().getMiddleName()+" "+sc.getPersonalInfo().getLastName());
				col.add(roomRes.getContent());
				col.add(sc.getPersonalInfo().getSex());
				col.add(sc.getPersonalInfo().getBirthday());
				col.add(sc.getPersonalInfo().getDocType());
				col.add(sc.getPersonalInfo().getDocNumber());
				col.add(sc.getPersonalInfo().getAddress());
				col.add(sc.getNote());
				ret.add(col);
			}
		}
		else 
		{
			try {
				resultList.addAll(ClientSystemHibernateInterface.getSingleClientsInHotel(session));
			}catch(Exception e){return null;}
			if (resultList.size() == 0)
				return null;
			else
				for (int i = 0 ;i < resultList.size();i++)
				{
					SingleClient sc = (SingleClient)resultList.get(i);
					LinkedList col = new LinkedList();
					col.add(sc.getPersonalInfo().getFirstName()+" "+sc.getPersonalInfo().getMiddleName()+" "+sc.getPersonalInfo().getLastName());
					col.add(roomRes.getContent());
					col.add(sc.getPersonalInfo().getSex());
					col.add(sc.getPersonalInfo().getBirthday());
					col.add(sc.getPersonalInfo().getDocType());
					col.add(sc.getPersonalInfo().getDocNumber());
					col.add(sc.getPersonalInfo().getAddress());
					col.add(sc.getNote());
					ret.add(col);
				}
		}
		return ret;
	}
}
